package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.core.Pager;
import com.campus.mapper.ManagerActivityMapper;
import com.campus.mapping.ManagerActivityMapping;
import com.campus.model.dto.PageResult;
import com.campus.model.TManagerActivity;
import com.campus.model.dto.ManagerActivityConditionDto;
import com.campus.model.dto.ManagerActivityCreateDto;
import com.campus.model.dto.ManagerActivityUpdateDto;
import com.campus.service.IManagerActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ManagerActivityServiceImpl extends ServiceImpl<ManagerActivityMapper,TManagerActivity>
implements IManagerActivityService {

    @Autowired
    private ManagerActivityMapper managerActivityMapper;

    @Override
    public Integer createActivity(ManagerActivityCreateDto managerActivityCreateDto) {
        TManagerActivity tManagerActivity = ManagerActivityMapping.INSTANCE.CreatetoActivity(managerActivityCreateDto);
        return managerActivityMapper.insert(tManagerActivity);
    }

    @Override
    public Integer updateManagerActivity(ManagerActivityUpdateDto managerActivityUpdateDto) {
        TManagerActivity tManagerActivity = ManagerActivityMapping.INSTANCE.UpdatetoActivity(managerActivityUpdateDto);
        return managerActivityMapper.updateById(tManagerActivity);
    }



    @Override
    @Transactional
    public Integer publishActivity(Long id) {
        TManagerActivity tManagerActivity = managerActivityMapper.selectById(id);
        tManagerActivity.setPublishTime(new Date());
        tManagerActivity.setStatus(1L);

        return managerActivityMapper.updateById(tManagerActivity);
    }

    @Override
    public TManagerActivity getActivityById(Long id) {
        return managerActivityMapper.selectById(id);
    }

    @Override
    public Page<TManagerActivity> selectActivitiesByConditions(ManagerActivityConditionDto managerActivityConditionDto, Pager pager) {

        Page<TManagerActivity> page = new Page<>(pager.getPageNum(), pager.getPageSize());

        //联表查询不能使用lambda表达式构造器
        QueryWrapper<TManagerActivity> qw = new QueryWrapper<>();

        qw.like(StringUtils.isNotEmpty(managerActivityConditionDto.getName()),"name",managerActivityConditionDto.getName());
        qw.eq(managerActivityConditionDto.getStatus() != null,"status",managerActivityConditionDto.getStatus());
        qw.like(StringUtils.isNotEmpty(managerActivityConditionDto.getTitle()),"title",managerActivityConditionDto.getTitle());
        qw.like(StringUtils.isNotEmpty(managerActivityConditionDto.getCreator()),"creator",managerActivityConditionDto.getCreator());
        qw.like(StringUtils.isNotEmpty(managerActivityConditionDto.getCategory()),"category",managerActivityConditionDto.getCategory());
        qw.orderByDesc("publish_time");

        return managerActivityMapper.selectPage(page,qw);
    }
}
